package com.example.inoculations.controller;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.inoculations.entity.Forum;
import com.example.inoculations.entity.Reservation;
import com.example.inoculations.entity.User;
import com.example.inoculations.mapper.ForumMapper;
import com.example.inoculations.resultR.R;
import com.example.inoculations.service.IForumService;
import com.example.inoculations.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author meirui
 * @since 2022-11-19
 */
@Controller
@ResponseBody
@RequestMapping("/inoculations/forum")
public class ForumController {
    @Autowired
    IForumService iForumService;
    @Autowired
    IUserService iUserService;
    /*分页查询*/
    @GetMapping("/pageall")
    public R<IPage<Forum>> getpageall(@RequestParam("pageNum") Integer pageNum,
                                      @RequestParam("pageSize") Integer pageSize,
                                      @RequestParam(required = false) String userid,
                                      @RequestParam(required = false) String title) {

        IPage<Forum> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Forum> queryWrapper = new QueryWrapper<>();
        /*根据身份证号模糊查询*/
        if (!StringUtils.isEmpty(userid)){
            queryWrapper.like("userid",userid);
        }
        /*根据标题模糊查询*/
        if (!StringUtils.isEmpty(title)){
            queryWrapper.like("title",title);
        }
        queryWrapper.orderByDesc("id");//根据id倒序
        IPage<Forum> page1 = iForumService.page(page, queryWrapper);
//        根据用户id查询用户的名称
        List<Forum> records = page1.getRecords();
        for(Forum it: records) {
            String idcard = it.getUserid();
            if (idcard != null) {
                QueryWrapper<User> qw = new QueryWrapper();
                qw.eq("idcard", idcard);
                User user = iUserService.getOne(qw);
                if (user != null) {
                    it.setUser(user);
                }
            }
        }
        return R.SUCCESS(page1);
    }
    /*添加和修改，根据是否传入id，有则修改，无则添加*/
    @PostMapping("/save")
    public R save(@RequestBody Forum forum){
        if (iForumService.saveOrUpdate(forum)){
            return R.SUCCESS();
        }else {
            return R.FAIL();
        }
    }
    /*删除*/
    @DeleteMapping("/del/{id}")
    public R del(@PathVariable Integer id){
        if (iForumService.removeById(id)){
            return R.SUCCESS();
        }else {
            return R.FAIL();
        }
    }
//    根据id查询论坛文章详情,同时查询文章作者信息
    @GetMapping("get/{id}")
    public R getone(@PathVariable Integer id) {
        Forum forum = iForumService.getById(id);
        if(forum!=null){
            String userid = forum.getUserid();
            //System.out.println(userid);
            QueryWrapper<User> qw = new QueryWrapper();
            User user = iUserService.getOne(qw.eq("idcard",userid));
            if (user!=null){
                forum.setUser(user);
                //System.out.println(user);
            }
//            点击数量自增
            UpdateWrapper wrapper = new UpdateWrapper();
            wrapper.setSql("clicknum = clicknum+ 1");
            wrapper.eq("id", id);
            iForumService.update(wrapper);

//            Integer clicknum = forum.getClicknum();
//            clicknum++;
//            UpdateWrapper wrapper = new UpdateWrapper();
//            wrapper.eq("id", id);
//            wrapper.set("clicknum",clicknum);
//            iForumService.update(wrapper);

        }
        return R.SUCCESS(forum);
    }
}
